package thread;

import java.io.Serializable;

/**
 * @author DBX
 * @effect 线程一输出奇数，线程二输出偶数
 * @date 2021/09/11/14:18
 */
public class Test4 {
    public static int i = 0 ;

    public static void main(String[] args) {

        Test4 test4 = new Test4();

        Thread t1 = new Thread(new Odd(test4));
        t1.start();
        Thread t2 = new Thread(new even(test4));
        t2.start();

    }

}

class Odd implements Runnable{
    private Test4 test4;

    public Odd(Test4 test4) {
        this.test4 = test4;
    }

    @Override
    public void run() {
        while (true){
            synchronized (test4) {
                if (test4.i%2==0){

                    try {
                        test4.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                test4.i = test4.i+1;
                System.out.println(Thread.currentThread().getName() + "---->"+(test4.i));
                test4.notifyAll();
            }

        }
    }
}
class even implements Runnable{
    public even(Test4 test4) {
        this.test4 = test4;
    }

    private Test4 test4;
    @Override
    public void run() {
        while (true){
                synchronized (test4) {
            if (test4.i%2==1){

                    try {
                        test4.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                    test4.i = test4.i+1;
                    System.out.println(Thread.currentThread().getName() + "---->"+(test4.i));
                    test4.notifyAll();
        }

    }}
}